package com.suli.account.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suli.account.entity.SaleAndCount;
import com.suli.account.entity.SalesQuantity;
import com.suli.account.entity.ScenicTicket;
import com.suli.account.mapper.SalesQuantityMapper;
import com.suli.account.mapper.ScenicTicketMapper;
import com.suli.account.mapper.UserTicketMapper;
import com.suli.account.service.ScenicTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuLi
 * @since 2022-04-14
 */
@Service
public class ScenicTicketServiceImpl extends ServiceImpl<ScenicTicketMapper, ScenicTicket> implements ScenicTicketService {

    @Autowired
    UserTicketMapper ticketMapper;
    @Autowired
    SalesQuantityMapper quantityMapper;

    @Override
    public Map<String, Object> getBetweenDays(Date startTime,Date endTime) {
        QueryWrapper<SalesQuantity> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("buy_date", startTime, endTime);
        List<SalesQuantity> salesQuantities = quantityMapper.selectList(queryWrapper);
        Map<String, Object> map=new HashMap<>();
        map.put("saleCount", salesQuantities);
        return map;
    }

    @Override
    public Map<String, Object> getSalesAndRate(Date startTime, Date endTime) {
        QueryWrapper<SalesQuantity> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("buy_date", startTime, endTime);
        List<SalesQuantity> salesQuantities = quantityMapper.selectList(queryWrapper);
        List<SaleAndCount> list=new ArrayList<>();
        for (int i = 0; i < salesQuantities.size(); i++) {
            SaleAndCount saleAndCount=new SaleAndCount();
            saleAndCount.setBuyDate(salesQuantities.get(i).getBuyDate());
            saleAndCount.setQuantity(salesQuantities.get(i).getSalesVolume());
            saleAndCount.setSales(salesQuantities.get(i).getSales());
            if(i==0){
                saleAndCount.setQuantityRate(0.00);
                saleAndCount.setSalesRate(0.00);
            }else {
                Double quantityRate = getRate(salesQuantities.get(i).getSalesVolume(), salesQuantities.get(i - 1).getSalesVolume());
                double v = salesQuantities.get(i).getSales().doubleValue() - salesQuantities.get(i - 1).getSales().doubleValue();
                double v1 = v / salesQuantities.get(i - 1).getSales().doubleValue();
                v=(double) Math.round(v*100)/100;
                v1=(double) Math.round(v1*100)/100;
                saleAndCount.setQuantityRate(quantityRate);
                saleAndCount.setSalesRate(v1);

            }
            list.add(saleAndCount);
        }
        Map<String, Object> map=new HashMap<>();
        map.put("salesQuantity", list);
        return map;
    }

    /**
     * 获取变化率
     * @param a i
     * @param b i-1
     * @return 变化率
     */
    private Double getRate(Integer a,Integer b){
        double c=(a.doubleValue()-b.doubleValue())/b.doubleValue();
        c=(double) Math.round(c*100)/100;
        return c;
    }
}
